package com.idbk.chargestation.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;

import com.idbk.chargestation.AppContext;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationURL;
import com.idbk.chargestation.util.TencentUtil;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

/**
 * 自定义分享Dialog
 *
 * @author liangzw, zhx
 */

public class DialogShare extends Dialog implements View.OnClickListener {

    private Context context;
    private String name;//分享名称
    private String url;//分享链接

    public DialogShare(Context context, String url, String name) {
        super(context);
        this.context = context;
        this.url = url;
        this.name = name;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (getWindow() != null) {
            //设置dialog默认背景为透明
            getWindow().setBackgroundDrawableResource(android.R.color.transparent);
            getWindow().setGravity(Gravity.BOTTOM);//设置Dialog显示在下方
            getWindow().setWindowAnimations(R.style.share_dialog);//设置显示动画
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            View mView = inflater.inflate(R.layout.dialog_share, null);//dialog布局显示
            setContentView(mView);
            //设置Dialog横向全屏,纵向匹配内容
            getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

        }
        setupView();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.dismiss();
        return super.onTouchEvent(event);
    }

    private void setupView() {
        findViewById(R.id.share_friends).setOnClickListener(this);
        findViewById(R.id.share_wechat).setOnClickListener(this);
        findViewById(R.id.share_qq).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.share_friends:
                share2weixin(1);//朋友圈
                break;

            case R.id.share_wechat:
                share2weixin(0);//微信分享
                break;

            case R.id.share_qq:
                share2Tencent();//QQ分享
                break;
            default:
                break;
        }
    }

    //微信
    private void share2weixin(int flag) {
        IWXAPI msgApi = AppContext.getInstance().getWxApi();
        //判断是否安装微信
        if (!msgApi.isWXAppInstalled()) {
            Toast.makeText(context, "您还未安装微信客户端",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        //构造
        WXWebpageObject webpage = new WXWebpageObject();
        webpage.webpageUrl = url;
        //
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = webpage;
        msg.title = name;
        msg.description = "点击进入查看此充电桩";
        Bitmap thumb = BitmapFactory
                .decodeResource(context.getResources(), R.drawable.logo);
        msg.setThumbImage(thumb);
        //构造一个req
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = String.valueOf(System.currentTimeMillis());
        req.message = msg;
        req.scene = flag;
        msgApi.sendReq(req);
    }

    //QQ
    private void share2Tencent() {
        Tencent mTencent = Tencent.createInstance(
                context.getResources().getString(R.string.qq_share_key),
                context);
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);//分享消息：图文
        params.putString(QQShare.SHARE_TO_QQ_TITLE, name);
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "点击进入查看此充电桩");
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, url);
        String imageUrl = ChargeStationURL.getAbsoluteApiUrlFromRoot("/uploads/user/finally_logo.png");
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, imageUrl);//图片URL地址
        mTencent.shareToQQ((Activity) context, params, qqShareListener);
    }

    private IUiListener qqShareListener = new IUiListener() {
        @Override
        public void onCancel() {
            // TODO Auto-generated method stub
            TencentUtil.toastMessage((Activity) context, "onCancel: ");
        }

        @Override
        public void onComplete(Object response) {
            // TODO Auto-generated method stub
            TencentUtil.toastMessage((Activity) context, "onComplete: " + response.toString());
        }

        @Override
        public void onError(UiError e) {
            // TODO Auto-generated method stub
            TencentUtil.toastMessage((Activity) context, "onError: " + e.errorMessage, "e");
        }
    };

}
